package com.coocaa.ops.admin.v1.audit;


import com.coocaa.ops.admin.base.language.CommonLanguageProperties;
import com.coocaa.ops.admin.v1.audit.converter.AuditTaskConverter;
import com.coocaa.ops.admin.v1.audit.service.IAuditCommonService;
import com.coocaa.ops.common.core.biz.audit.entity.AuditTaskInfoEntity;
import com.coocaa.ops.common.core.biz.audit.entity.AuditTaskInfoHistoryEntity;
import com.coocaa.ops.common.core.biz.auth.entity.SysUserEntity;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;

import java.util.List;

/**
 * @author bijiahao
 * @date : 2018/5/30.
 * @description: 审核相关的公共服务 服务工具类
 */
@Slf4j
public class AuditUtil {
    @Setter
    private static IAuditCommonService auditCommonService;
    private static CommonLanguageProperties commonLanguageProperties;

    public static void updateById(AuditTaskInfoEntity entity) {
        getAuditCommonService().updateById(entity);
    }

    public static void saveAuditTaskInfoHistory(AuditTaskInfoEntity entity) {
        getAuditCommonService().saveAuditTaskInfoHistory(entity);
    }

    /**
     * 打开任务
     *
     * @param taskName
     * @param resourceId
     * @param resourceVersion
     * @param resourceType
     * @param source
     * @param user
     * @param priority
     * @return
     */
    public static AuditTaskInfoEntity openTask(String taskName, Integer resourceId, Integer resourceVersion, String resourceType, String source, SysUserEntity user, Integer priority) {
        AuditTaskInfoEntity openTaskInfo = AuditTaskConverter.convertOpenTaskInfo(taskName, resourceId, resourceVersion, resourceType, source, priority);
        return getAuditCommonService().openTask(openTaskInfo, user);
    }

    /**
     * 获取任务信息-通过资源ID-版本-类别
     *
     * @param resourceId
     * @param resourceVersion
     * @param resourceType
     * @return
     */
    public static AuditTaskInfoEntity getTaskByIdAndVersionAndType(Integer resourceId, Integer resourceVersion, String resourceType) {
        AuditTaskInfoEntity taskInfoEntity = getAuditCommonService().getTaskByIdAndVersionAndType(resourceId, resourceVersion, resourceType);
        return taskInfoEntity;

    }

    /**
     * 查询审核历史信息-通过资源ID-版本-类别
     *
     * @param resourceId
     * @param version
     * @param resourceType
     * @return
     */
    public static List<AuditTaskInfoHistoryEntity> queryAuditHistory(Integer resourceId, Integer version, String resourceType) {
        return getAuditCommonService().queryAuditHistory(resourceId, version, resourceType);
    }

    /**
     * 删除任务信息-通过资源ID-版本-类别
     *
     * @param id
     * @param version
     * @param type
     */
    public static void logicDelByIdAndVersionAndType(Integer id, Integer version, String type) {
        getAuditCommonService().logicDelByIdAndVersionAndType(id, version, type);
    }

    /**
     * 校验服务注入情况
     *
     * @return
     */
    private static IAuditCommonService getAuditCommonService() {
        Assert.notNull(auditCommonService, "IAuditCommonService instance " + commonLanguageProperties.getNotNull());
        return auditCommonService;
    }

    @Autowired
    public void setCommonLanguageProperties(CommonLanguageProperties commonLanguageProperties) {
        AuditUtil.commonLanguageProperties = commonLanguageProperties;
    }

}
